<?php

class Damages_Methods_Request_SetCarDamageDiscard extends Damages_Methods_Request {

  /**
   *
   * @var int
   */
  public $id;

  /**
   *
   * @var int
   */
  public $discardId;

  /**
   *
   * @var string 
   */
  public $discardDescription;

  /**
   *
   * @var int
   */
  public $statusId;

  public function __construct($params) {
    parent::__construct($params);
    $this->id = (int) $params->id;
    $this->discardId = (int) $params->discardId;
    $this->statusId = (int) $params->statusId;
    $this->discardDescription = $params->discardDescription;
  }

  public function process() {
    // odrzucenie szkody

    if (!Damages_Auth::getInstance()->checkPermission(array(Damages_Auth::PERMISSION_LS, Damages_Auth::PERMISSION_ADMIN)))
      throw new Damages_Exception_App(Damages_Codes::ACCESS_DENIED);
    $data = $this->getDbObject();
    if ($data) {
      $data->setDiscardId($this->discardId);
      $data->setDiscardDescription($this->discardDescription);
      $data->setStatusId($this->statusId);
      $data->save();
    }
    else
      throw new Damages_Exception_App(Damages_Codes::DB_OBJECT_NOT_FOUND);

    if ($data->getProcessTypes()->getCode() == DamagesProcessTypes::CODE_DOMESTIC) {
      $this->sendDealerNotificationEmail($data);
    }
    $response = new Damages_Methods_Response(true);
    return $response;
  }

  /**
   * 
   * @return DamagesCarDamages
   */
  private function getDbObject() {
    $data = DamagesCarDamagesQuery::create()->findOneById($this->id);
    return $data;
  }

  private function sendDealerNotificationEmail(DamagesCarDamages $carDamage) {
    if (!$carDamage->getCarData()->getDealersRelatedByDealerId())
      return false;
    if ($carDamage->getCarData()->getDealersRelatedByDealerId()->getUserDealerss()->isEmpty())
      return false;

        
    $viewDir = Zend_Controller_Front::getInstance()->getModuleDirectory() . '/views/scripts/partials/';
    $view = new Zend_View();
    $view->setScriptPath($viewDir);
    $view->carDamage = $carDamage;
    $view->toVerification = $carDamage->getDictStatuses()->getCode() == DamagesDictStatuses::CODE_DISCARD_TO_VERIFY ? true : false;
    $template = DamagesEmailTemplatesQuery::create()->findOneByCode(DamagesEmailTemplates::CODE_DEALER_DISCARD);
    $templateContent = $template->getTemplate();
    $templateContent = nl2br($templateContent);
    foreach (Damages_Tags::getInstance()->getList() as $value) {
      if (preg_match(sprintf(Damages_Emails::TEMPLATE_REGEX, $value->code), $templateContent)) {
        $data = $view->render($value->code . '.phtml');
        $templateContent = preg_replace(sprintf(Damages_Emails::TEMPLATE_REGEX, $value->code), $data, $templateContent);
      }
    }
    $dealerEmail = '';
    foreach ($carDamage->getCarData()->getDealersRelatedByDealerId()->getUserDealerss() as $userDealer) {
      $dealerEmail = $dealerEmail ? sprintf('%s,%s',$dealerEmail,$userDealer->getUsers()->getEmail()) : $userDealer->getUsers()->getEmail();
    }
//    $dealerEmail = $carDamage->getCarData()->getDealersRelatedByDealerId()->getUsers()->getEmail();
    Damages_Emails::addEmail($carDamage->getId(), $template->getId(), $templateContent, $dealerEmail);
  }

}